package com.tj.jinzao.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.tj.jinzao.R;
import com.tj.jinzao.activity.LoginActivity;
import com.tj.jinzao.activity.MainActivity;
import com.tj.jinzao.base.BaseFragment;
import com.tj.jinzao.callback.IRegisterLoginView;
import com.tj.jinzao.manager.UserManager;
import com.tj.jinzao.model.User;
import com.tj.jinzao.presenter.RegisterLoginPresenter;
import com.tj.jinzao.utils.ToastUtils;

import butterknife.BindView;
import butterknife.OnClick;

import static com.tj.jinzao.activity.LoginActivity.LOGIN_TYPE_INVITE;
import static com.tj.jinzao.activity.LoginActivity.LOGIN_TYPE_PASSWORD_LOGIN;

/**
 * 重置密码
 * Created by wjy on 2019/1/5.
 */
public class SetPasswordFragment extends BaseFragment<RegisterLoginPresenter> implements IRegisterLoginView<User> {

    public final static String ARG_TOKEN = "token";
    public final static String ARG_CODE = "code";
    public final static String ARG_PHONE = "phone";
    public final static String ARG_INPUT_TYPE = "input_type";
    @BindView(R.id.tv_title)
    TextView mTitleView;
    @BindView(R.id.tv_new_password)
    TextView mNewTitleView;
    @BindView(R.id.et_password_1)
    EditText mPassword1View;
    @BindView(R.id.et_password_2)
    EditText mPassword2View;
    @BindView(R.id.iv_back_lay)
    RelativeLayout iv_back_lay;

    public int mType;//1设置密码，2重置密码
    private String mToken, mPhone, mSmsCode;

    public static SetPasswordFragment newInstance(String token, String phone, String smsCode, int type) {
        SetPasswordFragment fragment = new SetPasswordFragment();
        Bundle args = new Bundle(3);
        args.putInt(ARG_INPUT_TYPE, type);
        args.putString(ARG_TOKEN, token);
        args.putString(ARG_CODE, smsCode);
        args.putString(ARG_PHONE, phone);
        fragment.setArguments(args);
        return fragment;
    }
    @OnClick({R.id.iv_see_1, R.id.iv_see_2})
    public void showPassword(View view) {
        EditText editText = view.getId() == R.id.iv_see_1 ? mPassword1View : mPassword2View;

        if (EditorInfo.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD == editText.getInputType()) {
            //如果不可见就设置为可见
            editText.setInputType(EditorInfo.TYPE_TEXT_VARIATION_PASSWORD);
            editText.setTransformationMethod(PasswordTransformationMethod.getInstance());
            if (view instanceof ImageView) {
                ((ImageView) view).setImageResource(R.mipmap.login_unsee);
            }
        } else {
            //如果可见就设置为不可见
            editText.setInputType(EditorInfo.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
            editText.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            if (view instanceof ImageView) {
                ((ImageView) view).setImageResource(R.mipmap.login_see);
            }
        }
        //执行上面的代码后光标会处于输入框的最前方,所以把光标位置挪到文字的最后面
        editText.setSelection(editText.getText().toString().length());
    }

    @Override
    protected void setupViews(LayoutInflater inflater, View view) {
        super.setupViews(inflater, view);
        mType = getArguments().getInt(ARG_INPUT_TYPE);
        if (mType == 2) {
            mTitleView.setText("重置密码");
            mNewTitleView.setText("登录密码");
            mToken = getArguments().getString(ARG_TOKEN);
            mPhone = getArguments().getString(ARG_PHONE);
            mSmsCode = getArguments().getString(ARG_CODE);
        }
        iv_back_lay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ((LoginActivity) mContext).switchFragment(LoginActivity.LOGIN_TYPE_PASSWORD_LOGIN);
            }
        });
    }

    @Override
    protected RegisterLoginPresenter createPresenter() {
        return new RegisterLoginPresenter(this);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_set_password;
    }
    public  void setArgToken(String mToken,String mPhone,String mSmsCode,int mType){
        this.mToken=mToken;
        this.mPhone=mPhone;
        this.mSmsCode=mSmsCode;
        this.mType=mType;
    }


    @OnClick(R.id.bt_confirm)
    public void confirm() {
        String psd1 = mPassword1View.getText().toString().trim();
        String psd2 = mPassword2View.getText().toString().trim();
        if (TextUtils.isEmpty(psd1)) {
            ToastUtils.showToast(mContext, "请输入密码");
        } else if (TextUtils.isEmpty(psd2)) {
            ToastUtils.showToast(mContext, "请再次输入密码");
        } else if ((psd1.length() < 6 || psd1.length() > 18) || (psd2.length() < 6 || psd2.length() > 18)) {
            ToastUtils.showToast(mContext, "密码为6-18位字母或数字");
        } else if (!TextUtils.equals(psd2, psd1)) {
            ToastUtils.showToast(mContext, "两次输入密码不正确");
        } else {
            if (mType == 1) {
                mPresenter.setPassword(psd1);
            } else {
                mPresenter.resetPassword(mToken, mPhone, mSmsCode, psd1);
            }
        }

    }

    @Override
    public void onRequestSuccess(User user) {
        if (mType == 1) {
            if (mContext instanceof LoginActivity) {
                if(UserManager.getInstance().getUser().getGender() == 3){
                    ((LoginActivity) mContext).switchFragment(LOGIN_TYPE_INVITE);
                }else {
                    startActivity(new Intent(mContext, MainActivity.class));
                    mContext.finish();
                }
            }
        }else {
            if (mContext instanceof LoginActivity) {
                ToastUtils.showToast(getActivity(),"密码重置成功");
                ((LoginActivity) mContext).switchFragment(LOGIN_TYPE_PASSWORD_LOGIN);
            }
        }
    }

    @Override
    public void onRequestNoData(String message) {

    }

    @Override
    public void onRequestError(String error_message) {
    }

    @Override
    public void onGetCodeSuccess(String s) {
        //发送验证码成功，此处不用
    }

    @Override
    public void onGetCodeFailed(String reason) {
        //发送验证码失败，此处不用
    }

    @Override
    public void onCheckCodeSuccess(String token) {
        //验证码通过
    }
}
